<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>角色修改</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" />
  <link rel="stylesheet" href="css/addActor.css">
  <script src="lib/js/jquery-1.12.4.js"></script>
  <script src="js/loginText.js"></script>
</head>

<body>
  <div class="myTable">
    <form id="editActor">

      <div class="form-group">
        <label for="exampleInputEmail1">旧角色名称</label>
        <input type="text" class="form-control" id="oldName" name="oldName" disabled>
      </div>

      <div class="form-group">
        <label for="exampleInputEmail1">请输入新角色名称</label>
        <input type="text" class="form-control" id="newName" name="newName">
      </div>
      <button type="submit" class="btn btn-primary">提交</button>
    </form>
  </div>
  

  <script>
    window.onload = function () {
      // 获取url传过来的参数,当表单默认数据
      var urlData = decodeURIComponent(window.location.href)
      name = urlData.split('?')[1].split('=')[1]
      $('#oldName')[0].value = name
      $('#newName')[0].value = name



      // 表单提交事件
      $("#editActor").submit(function (e) {
        e.preventDefault()
        var oldName = $('#oldName')[0].value.trim()
        var newName = $('#newName')[0].value.trim()
        console.log(oldName)
        console.log(newName)
        if (!newName) {
          window.alert('角色名不能为空！')
          return
        }

        if (confirm("确认修改？")) {

          $.ajax({
            url: '/pages/actor/editActor',
            type: 'post',
            data: JSON.stringify({
              oldName,
              newName 
            }),
            dataType: 'json',
            contentType:'application/json;charset=UTF-8',
            success: function (res) {
              console.log(res)
              if (res.success) {
                self.location = 'actor.html'
              } else if (res.error){
                window.alert(res.error)
              }
            }
          })

        } else {
          return
        }
      })

      





    }
  </script>
  <script src="lib/js/template-native.js"></script>
  <script src="lib/js/bootstrap.js"></script>


  
  <script src="lib/js/mock.js"></script>
  <script src="mock/editActor.js"></script>
</body>

</html>